package com.yuezhu.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.yuezhu.Model.PlanModel;
import com.yuezhu.Model.ProjectModel;
import com.yuezhu.Model.UserInfoModel;
import com.yuezhu.R;
import com.yuezhu.been.DataChangedEvent;
import com.yuezhu.been.UserMsg;
import com.yuezhu.constante.BusAction;
import com.yuezhu.control.TaskEngine;
import com.yuezhu.fragment.PartitionnformationFragment;
import com.yuezhu.global.ParallaxSwipeBackActivity;
import com.yuezhu.util.LoginOut;
import com.yuezhu.util.MyToast;
import com.yuezhu.util.StringUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.Collections;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class AccountInfoActivity extends ParallaxSwipeBackActivity {
    @InjectView(R.id.bottom_left)
    View bottom_left;
    @InjectView(R.id.bottom_middle)
    View bottom_middle;
    @InjectView(R.id.bottom_right)
    View bottom_right;
    @InjectView(R.id.iv_bottome_left)
    ImageView ivBottomeLeft;
    @InjectView(R.id.iv_bottome_middle)
    ImageView ivBottomeMiddle;
    @InjectView(R.id.iv_bottome_right)
    ImageView ivBottomeRight;
    @InjectView(R.id.iv_menu)
    ImageView ivMenu;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.iv_quite)
    ImageView ivQuite;
    @InjectView(R.id.tv_version)
    TextView tvVersion;
    @InjectView(R.id.bt_reset_password)
    Button btResetPassword;
    @InjectView(R.id.bt_quit_login)
    Button btQuitLogin;
    @InjectView(R.id.tv_modify_account)
    TextView tvModifyAccount;
    @InjectView(R.id.userName)
    TextView userName;
    @InjectView(R.id.userName2)
    TextView userName2;
    @InjectView(R.id.userEmail)
    TextView userEmail;
    @InjectView(R.id.userCellphone)
    TextView userCellphone;
    @InjectView(R.id.userCompany)
    TextView userCompany;
    @InjectView(R.id.userJob)
    TextView userJob;
    private String url;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_info);
        ButterKnife.inject(this);
        initView();
        setListeners();
        TaskEngine.getInstance().startGetUserInfoTask(queue, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject json) {
                try {
                    String respCode = json.getString("respCode");
                    if (!StringUtil.equal(respCode, "OK")) {
                        try {
                            if (json.getInt("respNo") == 999) {
                                LoginOut.LoginOut(AccountInfoActivity.this);
                            }
                        } catch (Exception e) {
                            MyToast.showDefaltToast("获取数据失败");
                        }
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    } else {
                        UserInfoModel model = new Gson().fromJson(json.getString("userInfo").toString(), UserInfoModel.class);
                        setText(userName, model.userName);
                        setText(userName2, model.userRealName);
                        setText(userEmail, model.userEmail);
                        setText(userCellphone, model.userCellphone);
                        setText(userCompany, model.userCompany);
                        setText(userJob, model.userJob);
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    MyToast.showDefaltToast("获取数据失败");
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.showDefaltToast("获取数据失败");
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(this);  //先得到构造器
        builder.setTitle("提示"); //设置标题
        builder.setMessage("更新中......"); //设置内容
        builder.setCancelable(false);
        dialog = builder.create();
        dialog.show();
    }

    private void setText(TextView textView, String s) {
        if (TextUtils.isEmpty(s)) {
            textView.setText("无");
        } else {
            textView.setText(s);
        }
    }

    @Override
    protected void initView() {
        ivMenu.setImageResource(R.drawable.zhuoyun);
        tvTitle.setText("账户详细信息");
        ivQuite.setVisibility(View.GONE);
        ivBottomeLeft.setVisibility(View.GONE);
        bottom_left.setVisibility(View.GONE);
        ivBottomeMiddle.setVisibility(View.VISIBLE);
        bottom_middle.setVisibility(View.VISIBLE);
        ivBottomeRight.setVisibility(View.GONE);
        bottom_right.setVisibility(View.GONE);
        ivBottomeMiddle.setImageResource(R.drawable.back);
        tvModifyAccount.setText(Html.fromHtml("<u>" + "前往卓云官网修改账户信息" + "</u>"));
    }


    @Override
    protected void setListeners() {
        super.setListeners();
        ivBottomeMiddle.setOnClickListener(this);
        btQuitLogin.setOnClickListener(this);
        btResetPassword.setOnClickListener(this);
        tvModifyAccount.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_bottome_middle:
                finish();
                break;
            case R.id.bt_quit_login:
                UserMsg.getInstance().quitOut();
                EventBus.getDefault().post(new BusAction.quitApp());
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(AccountInfoActivity.this, LoginActivity.class));
                        finish();
                    }
                }, 1000);
                break;
            case R.id.tv_modify_account:
                url = "http://www.zcloud-online.com/userservice/accountinfo.jsp";
                toBrowser();
                break;
            case R.id.bt_reset_password:
                url = "http://www.zcloud-online.com/userservice/forgetpassword.jsp";
                toBrowser();
                break;
        }
    }

    private void toBrowser() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        startActivity(intent);
    }
}
